package com.luoyi.weather.impl;

import com.luoyi.weather.Obverse;
import com.luoyi.weather.Subject;

import java.util.ArrayList;

public class WeatherData implements Subject {

    private ArrayList obverses;

    private float temp;

    private float humidity;

    private float pressure;

    public WeatherData() {
        obverses = new ArrayList();
    }

    @Override
    public void registerObverse(Obverse o) {
        obverses.add(o);
    }

    @Override
    public void removeObverse(Obverse o) {
        int i = obverses.indexOf(o);

        if (i > 0){
            obverses.remove(i);
        }

    }

    @Override
    public void notifyObverse() {
        for (int i = 0; i < obverses.size(); i++){
            Obverse o = (Obverse) obverses.get(i);
            o.update(temp, humidity, pressure);
        }
    }

    public void measurementChange(){
        notifyObverse();
    }

    public void setMeasurement(float temp, float humidity, float pressure){
        this.temp = temp;
        this.humidity = humidity;
        this.pressure = pressure;
        measurementChange();
    }
}
